#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

const static char exe[] = "\\mingw64\\libexec\\git-core\\git";
const static char envName[] = {'H', 'O', 'M', 'E', '='};
#define MAX_PATH_LEN  256

int main(int argc, char** argv)
{
    char *cmd;
    int i, len;
    char path[MAX_PATH_LEN];
    char env[MAX_PATH_LEN + sizeof(envName)];

    len = GetModuleFileName(NULL, path, MAX_PATH_LEN);
    if(!len) return 0;
    for(i = 0; len && i < 2; len--)
    {
        if(path[len] == '\\')
        {
            path[len] = '\0';
            i++;
        }
    }
    // 由 env 指定环境变量 HOME 的值
    memcpy(env, envName, sizeof(envName));
    strcpy(env + sizeof(envName), path);
    strcat(env, "\\home");

    len = strlen(path) + strlen(exe) + 1;
    for(i = 1; i < argc; i++)
        len += strlen(argv[i]) + 1;
    cmd = malloc(len);
    if(!cmd) return 0;

    strcpy(cmd, path);
    strcat(cmd, exe);
    for(i = 1; i < argc; i++)
    {
        strcat(cmd, " ");
        strcat(cmd, argv[i]);
    }

    putenv(env);
    system(cmd);
    free(cmd);
    return 0;
}
